<?php

/**
 * 列出模型自定义字段
 * 
 * @package    01CMS
 * @subpackage plugin
 * @author     rolong at vip.qq.com
 * @version    1.0.0
 * @link       http://www.01cms.com
 */

function smarty_block_field ($params, $content, $smarty, &$repeat)
{
    static $fields;
    
    if (is_null($content))
    {
        $fields = $params['fields'];
    }
    if ($o = current($fields))
    {
        $smarty->assign('name', $o->name);
        $startPos = strpos($o->code, '<!--config{');
        $endPos = strpos($o->code, '}-->');
        if ($startPos === FALSE || $endPos === FALSE)
        {
            return '自定义字段配置出错';
        }
        
        $configString = substr($o->code, $startPos + 11, $endPos - $startPos - 11);
        $configArr = explode(';', $configString);
        foreach ($configArr as $configItem)
        {
            list ($k, $v) = explode('=', $configItem);
            $config[$k] = $v;
        }
        $o->code = substr($o->code, $endPos + 4);
        
        if ($config['type'] == 'FCKeditor')
        {
            $FCKeditor = lib('FCKeditor', $o->field);
            $items = explode(';', $o->code);
            foreach ($items as $item)
            {
                if (empty($item))
                {
                    continue;
                }
                list ($k, $v) = explode('=', $item);
                $FCKeditor->$k = $v;
                if (isset($params['data']->{$o->field}))
                {
                    $FCKeditor->Value = $params['data']->{$o->field};
                }
            
            }
            
            $o->code = $FCKeditor->CreateHtml();
        }
        else
        {
            if (isset($params['data']->{$o->field}))
            {
                $o->code = str_replace('$value', $params['data']->{$o->field}, $o->code);
            }
            else
            {
                $o->code = str_replace('$value', '', $o->code);
            }
        }
        $smarty->assign('code', stripcslashes($o->code));
        next($fields);
        $repeat = true;
    }
    else
    {
        $repeat = false;
    }
    return $content;
}
